package mybatis.web;

import mybatis.pojo.Student;
import mybatis.services.StudentService;
import mybatis.util.StrUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Author 汤舒青
 * @Version 1.0
 */

@WebServlet("/studentAdd")
public class StudentAddServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DiskFileItemFactory d = new DiskFileItemFactory();
        ServletFileUpload sp = new ServletFileUpload(d);
        sp.setHeaderEncoding("utf-8");


        List<FileItem> fileItems = null;
        try {
            fileItems = sp.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Student student = new Student();
        for (FileItem f : fileItems) {

            if (f.isFormField()) {

                switch (f.getFieldName()) {
                    case "sname":
                        student.setSname(f.getString("utf-8"));

                        break;
                    case "gender":
                        student.setSex(f.getString("utf-8"));
                        break;
                    case "tel":
                        student.setTelephone(f.getString("utf-8"));


                        break;
                    case "qq":
                        student.setQq(f.getString("utf-8"));

                        break;
                    case "wechat":
                        student.setWechat(f.getString("utf-8"));


                        break;
                    case "gradeID":
                        student.setGradeid(StrUtil.toInt(f.getString("utf-8")));
                        break;
                    case "enterDate":
                        student.setEnterdate(StrUtil.toDate(f.getString("utf-8")));
                        break;
                    case "city":
                        student.setFromcity(f.getString("utf-8"));
                        break;
                    case "birth":
                        student.setBirthday(StrUtil.toDate(f.getString("utf-8")));

                        break;
                    case "address":
                        student.setAddress(f.getString("utf-8"));


                        break;
                    case "psd":
                        student.setPass(f.getString("utf-8"));
                        break;
                    case "idcardpic":
                        student.setIdcardpic(f.getString("utf-8"));
                        break;

                }
            } else {

                if (f.getSize() < 1)
                    continue;

                String fileName = f.getName();
                String houzhui = fileName.substring(fileName.lastIndexOf('.'));
                String newName = UUID.randomUUID() + houzhui;

                ServletContext application = req.getServletContext();
                File newPic = new File(application.getRealPath("images"));
                if (newPic.exists() == false)
                    newPic.mkdirs();

                try {
                    f.write(new File(newPic, newName));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                student.setIdcardpic("images/" + newName);///!!!!!!!
            }
        }


        int add = StudentService.ME.add(student);
        if (add > 0) {

        } else {

        }
    }
}
